/*****************************************************************************************/
/* ECSE 426 - Microprocessor - Fall 2009
/* Lab Experiment 2 - UART Control
/* UART.C
/* @author: Danny Wu - 260238209 - B.Comp.Eng
/* @author: Han-Sheng Chin - 260209715 - B.Elec.Eng
/* @version: 09.09.18.1304 
/****************************************************************************************/
#include <msp430x14x.h>
#include <stdlib.h>
#include <cross_studio_io.h>
#include <uart.h>
#define GRID_SIZE 8
#define NUM_ELEMENTS 0
#define MASK '*'
#define TRUE 1
#define FALSE 0
#define DEBUG_MODE FALSE


void initUART(void){
  unsigned int i;
  WDTCTL = WDTPW + WDTHOLD;             // Stop WDT
  BCSCTL1 &= ~XT2OFF;                   // XT2OFF
  do {
    IFG1 &= ~OFIFG;                       // Clear OSCFault flag
    for (i = 0xFF; i > 0; i--);           // Time for flag to set
  }while ((IFG1 & OFIFG) != 0);          // OSCFault flag still set?                
  BCSCTL2 |= SELM1+SELS;                // MCLK = SMCLK = XT2 (safe)
  UCTL0 = CHAR;                         // 8-bit character
  UTCTL0 = SSEL1;                       // UCLK = SMCLK
  UBR00 = 0x8A;                         // 8Mhz/57600 - 0x8a
  UBR10 = 0x00;                         //
  UMCTL0 = 0xEF;                        // no modulation
  ME1 |= UTXE0;                         // Enable USART0 TXD
  P3SEL |= 0x10;                        // P3.4 = USART0 TXD
  P3DIR |= 0x10;                        // P3.4 output direction
  _EINT();                              // Enable interrupts
  return;
}//end main

/**
 * Generates a random char
 *
 *   
 */
char getRndChar(void){
  char rnd_element = 33 + rand() % 127;

  return rnd_element;
}//end rand


/**
 * print
 */
void print(char* text){
  int ctr;
  for(ctr=0;text[ctr] != '\0';ctr++)
    putChar(text[ctr]);

  return;
}//end print
/**
 * println with a \n before
 */
void println(char* text){
  int ctr;
  putChar('\n');
  for(ctr=0;text[ctr] != '\0';ctr++)
    putChar(text[ctr]);

  return;
}//end println

/**
 *
 */
char* getChar(void){

}//end getchar

/**
 *
 */
void putChar(char text){
  if(DEBUG_MODE){
    debug_printf("%c",text);
  }else{    
    while ((IFG1 & UTXIFG0) == 0);        // USART0 TX buffer ready?
    TXBUF0 = text;                        // RXBUF0 to TXBUF0
  }//end if else
  return;
}//end putchar

/**
 * Clears the terminal
 * by shifting the output up.
 */
void clearScreen(void){
  putChar(0xC);
  return;
}//end getchar


